<?php
class Material_Model_Tree extends Zen_Tree 
{
	public function __construct() {
		$table = new Zend_Db_Table ( 'material_tree' );
		$this->table = $table;
		$types=array(
			'root'=>array(
						'icon'=>'/cms/res/icons/drive.png',
						'label'=>'Каталог товаров',
						'valid_children'=>array('folder'),
						'start_drag'=>false,
						'move_node'=>false,
						'delete_node'=>false,
						'remove'=>false
						),
			'folder'=>array(
						'table'=> 'material_folder',
						'label'=> 'раздел',
						'view' => 'form.phtml', 
						'form' => 'Material_Form_Folder', 
						'icon' => '/cms/res/icons/folder.png',
						'valid_children'=>array('wares','folder'),
						'start_drag'=>true,
						'move_node'=>true,
						'delete_node'=>true,
						'remove'=>true
						),
			'wares'=>array(
						'table'=> 'material_wares',
						'label'=> 'материал',
						'view' => 'form.phtml', 
						'form' => 'Material_Form_Wares', 
						'icon' => '/cms/res/icons/cart.png',
						'valid_children'=>'none',
						'start_drag'=>true,
						'move_node'=>true,
						'delete_node'=>true,
						'remove'=>true
						)						
		);
		$this->setTypes ( $types );
	}
	
	public function getSelectData(){
		$data=array(0=>'Не выбрано');
		$disabled=array();
		$res=$this->getChildren(0);
		if($res){
			foreach ($res as $wares){
				if($wares['type']!='folder'){
					$wares['label']='-----'.$wares['label'];;
				}else{
					$disabled[]=$wares['nid'];
				}
				$wares['label']=preg_replace('/[\',\",\«,\»]/ui', "", $wares['label']);
				$data[$wares['nid']]=$wares['label'];
			}
			return array('data'=>$data,'disabled'=>$disabled);
		}
	}
	
	public function getTree() {
		$res = $this->select()
			->setPlain(false)
			->setDepth(0)
			->setParentId(0)
			->setColumns(array(
				'tree'=>array('id','parent_id','type'),
				'folder'=>array('label','alias','info'),
				'wares'=>array('label','images','info','alias'),
			))
			->getData("tree.active=1");
		return $res;
	}
		
}